package tom.spring.cloud.provider.controller;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.io.JsonStringEncoder;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.gson.JsonObject;
import com.google.gson.annotations.JsonAdapter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.web.bind.annotation.RestController;
import tom.spring.cloud.api.service.IHelloService;
import tom.spring.cloud.api.vo.HelloParam;

/**
 * 使用公共模块直接继承注解
 * @author ZHUFEIFEI
 */
@Slf4j
@RestController
public class HelloApiController implements IHelloService {
    @Override
    public String hello() {
        return "hello world!";
    }

    @Override
    public String timeout() {
        try {
            int n = RandomUtils.nextInt(3000);
            Thread.sleep(n);
            System.out.println("sleep : " + n);
        } catch (InterruptedException e) {
        }
        return "hello~";
    }

    @Override
    public String queryMap(HelloParam param) {
        ObjectMapper om = new ObjectMapper();
        try {
            return om.writeValueAsString(param);
        } catch (JsonProcessingException e) {
            log.error("", e);
        }
        return "error";
    }

}
